package com.txy.binarytree;

import java.util.LinkedList;
import java.util.Queue;

public class BinarySearchTree {
    TreeNode root;

    public void insertNode(int data) {
        root = insert(root, data);
    }

    private TreeNode insert(TreeNode node , int data) {
        if(node ==null) return new TreeNode(data);
        if(data < node.data) {
            node.leftChild = insert(node.leftChild, data);
        }else if(data > node.data) {
            node.rightChild = insert(node.rightChild ,data);
        }else node.data = data;
        return node;
    }

    public void preOrder(TreeNode node) {
        if(node == null) return ;
        System.out.println(node.data);
        preOrder(node.leftChild);
        preOrder(node.rightChild);
    }

    public void inOrder(TreeNode node) {
        if(node == null) return ;
        preOrder(node.leftChild);
        System.out.println(node.data);
        preOrder(node.rightChild);
    }


    public void postOrder(TreeNode node) {
        if(node == null) return ;
        preOrder(node.leftChild);
        preOrder(node.rightChild);
        System.out.println(node.data);
    }

    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            System.out.println(node.data);
            if(node.leftChild != null) queue.offer(node.leftChild);
            if(node.rightChild !=null) queue.offer(node.rightChild);
        }
    }

    public static void main(String[] args) {
        BinarySearchTree bst = new BinarySearchTree();
        bst.insertNode(10);
        bst.insertNode(8);
        bst.insertNode(11);
        bst.insertNode(7);
        bst.insertNode(9);
        bst.insertNode(12);

        TreeNode root = bst.root;
        System.out.println();
    }
}
